package com.mtw.bbs.fileAdmin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.fileAdmin.pojo.entity.FileEntity;
import com.mtw.bbs.fileAdmin.pojo.vo.FileVo;
import com.mtw.bbs.fileAdmin.service.FileService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件管理接口层
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/file")
public class FileAdminController {


    private final FileService fileService;




    /**
     * 管理页查询文件列表
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "分页大小", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "name", value = "文件名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "originName", value = "原始文件名称", paramType = "query", dataType = "string")
    })
    @ApiOperation(value = "文件列表", notes = "")
    public Result<IPage<FileVo>> list(@RequestParam(name = "page") int page,
                                      @RequestParam(name = "size") int size,
                                      @RequestParam(name = "name",required = false) String name,
                                      @RequestParam(name = "originName",required = false) String originName) {

        LambdaQueryWrapper<FileEntity> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), FileEntity::getName, name);
        lqw.like(StringUtils.isNotBlank(originName), FileEntity::getOriginName, originName);
        lqw.orderByDesc(FileEntity::getCreatedDate);
        Page<FileEntity> paged = fileService.page(new Page<>(page, size), lqw);
        IPage<FileVo> voPage = new Page<>(paged.getCurrent(),paged.getSize(),paged.getTotal());

        if (!CollectionUtils.isEmpty(paged.getRecords())) {
            try {
                List<FileVo> collect = paged.getRecords().stream().map(x -> {
                    FileVo vo = new FileVo();
                    BeanUtils.copyProperties(x, vo);
                    FileVo fileUrl = fileService.getFileUrl(x.getId());
                    vo.setUrl(fileUrl==null?null:fileUrl.getUrl());
                    return vo;
                }).collect(Collectors.toList());
                voPage.setRecords(collect);
            }catch (Exception e){
                log.error("文件列表获取文件url失败。",e);
                return Result.fail(e.getMessage());
            }
        }
        return Result.success(voPage);
    }




    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文件", notes = "传入id集合")
    public Result delete(@RequestBody String[] ids) {

        fileService.removeBatchByIds(Arrays.asList(ids));
        return Result.success();
    }

}
